package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.jlkf.dao.TblOrderMapper;
import com.jlkf.dao.TblPushMessageMapper;
import com.jlkf.dao.TblUserMapper;
import com.jlkf.pojo.TblOrder;
import com.jlkf.pojo.TblPushMessage;
import com.jlkf.pojo.TblUser;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.JiGuangPushUtils;

@Service
public class TimeTaskServiceImpl {
	@Autowired
	private TblPushMessageMapper tblPushMessageMapper;
	@Autowired
	private TblUserMapper tblUserMapper;
	@Autowired
	private TblOrderMapper tblOrderMapper;
	
	@Scheduled(cron = "0 0 12 * * ?") // 每天中午12点执行一次
	public void pushMessage(){
		Map<String, Object> map = new HashMap<String,Object>();
		List<TblPushMessage> list = tblPushMessageMapper.selectByOptime();
		List<TblUser> userInfo = tblUserMapper.getVipUserInfo(map); //vip会员
		List<TblUser> users = tblUserMapper.getIsNotVip(); //非vip用户
		for(TblPushMessage message:list){
			if(message.getgRang()==1){ //全部用户
				JiGuangPushUtils.pushAll(message.getgTitle());
				message.setgStatus(2);
			}else if(message.getgRang()==2){ //vip用户
				for(TblUser user:userInfo){
					TblOrder order = tblOrderMapper.getVipInfo(user.getgId());
					Date validity = new Date();
					if(order!=null){
						//判断vip是否过期
						if(validity.getTime()<=order.getgValidityTime().getTime()){ //未过期
							JiGuangPushUtils.pushTargetForPeople(user.getgId().toString(), message.getgTitle());
							message.setgStatus(2);
						}
					}
				}
			}else if(message.getgRang()==3){ //非vip用户
				for(TblUser user:users){ //未购买vip
					JiGuangPushUtils.pushTargetForPeople(user.getgId().toString(), message.getgTitle());
					message.setgStatus(2);
				}
				for(TblUser user:userInfo){ //购买了vip，判断是否过期
					TblOrder order = tblOrderMapper.getVipInfo(user.getgId());
					Date validity = new Date();
					if(order!=null){
						if(validity.getTime()>order.getgValidityTime().getTime()){
							JiGuangPushUtils.pushTargetForPeople(user.getgId().toString(), message.getgTitle());
							message.setgStatus(2);
						}
					}
				}
			}else if(message.getgRang()==4){ //年度会员
				for(TblUser user:userInfo){
					if(user.getgUserIsVip()==3){
						TblOrder order = tblOrderMapper.getVipInfo(user.getgId());
						Date validity = new Date();
						if(order!=null){
							//判断vip是否过期
							if(validity.getTime()<=order.getgValidityTime().getTime()){ //未过期
								JiGuangPushUtils.pushTargetForPeople(user.getgId().toString(), message.getgTitle());
								message.setgStatus(2);
							}
						}
					}
				}
			}else if(message.getgRang()==5){ //季度会员
				for(TblUser user:userInfo){
					if(user.getgUserIsVip()==2){
						TblOrder order = tblOrderMapper.getVipInfo(user.getgId());
						Date validity = new Date();
						if(order!=null){
							//判断vip是否过期
							if(validity.getTime()<=order.getgValidityTime().getTime()){ //未过期
								JiGuangPushUtils.pushTargetForPeople(user.getgId().toString(), message.getgTitle());
								message.setgStatus(2);
							}
						}
					}
				}
			}else if(message.getgRang()==6){  //月度会员
				for(TblUser user:userInfo){
					if(user.getgUserIsVip()==1){
						TblOrder order = tblOrderMapper.getVipInfo(user.getgId());
						Date validity = new Date();
						if(order!=null){
							//判断vip是否过期
							if(validity.getTime()<=order.getgValidityTime().getTime()){ //未过期
								JiGuangPushUtils.pushTargetForPeople(user.getgId().toString(), message.getgTitle());
								message.setgStatus(2);
							}
						}
					}
				}
			}
			tblPushMessageMapper.updateByPrimaryKeySelective(message);
		}
	}
}
